#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'
import time


def getCount(year):
    import requests
    import json

    url = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&mindate=' + str(
        year) + '&maxdate=' + str(year) + '&retmode=json'
    r = requests.get(url, timeout=180)
    # print r.content
    s = json.loads(r.content)
    return int(s["esearchresult"]["count"])


if __name__ == "__main__":
    outfile = open("pubmed_count.txt", "w")
    for i in xrange(1800, 2017):
        count = getCount(i)
        outfile.write(str(i) + "\t" + str(count) + "\n")
        outfile.flush()
        print str(i) + "\t" + str(count)
    outfile.close()
